<template>
  <div class="welcome"   >
    <div class="welcome">欢迎注册博客</div>
    <div class="welcome">每一天，乐在沟通。</div>
    <el-card style="width: 600px;height: 100%;margin: 0 auto;
background-color: rgba(255,255,255,0.6)">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm"   label-width="100px" class="deo-ruleForm"
               style="width: 400px;margin: 30px auto;"  >
        <el-form-item label="账  号" prop="username">
          <el-input v-model="ruleForm.username" maxlength="15"></el-input>
        </el-form-item>

        <el-form-item label="密  码" prop="password">
          <el-input type="password" v-model="ruleForm.password" maxlength="15" ></el-input>
        </el-form-item>

        <el-form-item label="昵 称" prop="nick">
          <el-input v-model="ruleForm.nick" maxlength="15" ></el-input>
        </el-form-item>

   、<el-form-item>
        <input type="text" placeholder="手机号"  v-model="phone" style="height: 20px" >
        <input  type="button" v-model="sendMsg"   @click="sendSms()" >
        </el-form-item>

      <el-form-item>
        <input type="text" placeholder="短信验证码" v-model="smsCode" style="height: 20px"  >
       <input  type="button" value="验证短信" @click="checkSms()">
      </el-form-item>

        <el-form-item label="性  别" prop="gender">
          <el-switch
              v-model="ruleForm.gender"
              active-color="#F56C6C"
              inactive-color="#409EFF"
              :active-value="0"
              :inactive-value="1"
              active-text="女"
              inactive-text="男"
              style="bottom: 1px;position: relative;right: 100px "
          ></el-switch>

        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>

    </el-card>
  </div>
</template>


<script>


export default {
  data() {

    return {
      ruleForm: {
        username: '',
        password: '',
        gender: 1,
        status: 1,
        phone: '',
        smsCode:''

      },
      //添加设置
      phone: '',
      smsCode:'',
      sendMsg:'发送短信',
      timeCount:'60',
      isSend: false,




      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {min: 11, max: 11, message: '长度在11 个字符', trigger: 'blur'}
        ],
      },      //验证规则
      genderClick: true,               // 性别
      AddOrSetByUserId: 0,              // 区别方法【添加 ro 修改】

      dialogVisibleTitle: '添加用户',   // 弹框标题---【添加 or 修改功能】
      dialogVisible: false,          // 弹出框状态【添加 or 修改功能】，默认隐藏
      noneBtnImg: false, // 隐藏上传按钮


    };
  },

  methods: {
    submitForm() {
      let addOrSetData = this.ruleForm;



      if (addOrSetData.username === null || addOrSetData.username === undefined || addOrSetData.username === '') {
        this.$message({type: 'error', message: "账号不能为空"})
        return;
      }
      if (addOrSetData.password === null || addOrSetData.password === undefined ||addOrSetData.password === '') {
        this.$message({type: 'error', message: "密码不能为空"})
        return;
      }
      if (this.phone === null || this.phone === undefined ||this.phone=== '') {
        this.$message({type: 'error', message: "电话号码不能为空"})
        return;
      }
      if (this.smsCode === null ||this.smsCode === undefined ||this.smsCode=== '') {
        this.$message({type: 'error', message: "验证码不能为空"})
        return;
      }


        let path = "http://localhost:9090/system/users/createUser"
        this.ruleForm.optionsSelect = this.optionsSelectEcho;
        this.axios.post(path, addOrSetData).then((response) => {
          switch (response.data.code) {
            case 200:
              this.$message({
                type: 'success',
                message: "创建成功"
              });
              this.$router.push('/login');
              // this.byUsername();
              break;
            default:
              this.$message({
                type: 'error',
                message: response.data.message
              })
              break;
          }
        })
      },

    sendSms() {
      if (this.phone == null || this.phone=='') {
        this.$message({
          type: 'error',
          message: "手机号不能为空"
        })
        return;
      }
      //验证手机号格式
      if (this.phone.substr(0, 2) != 13 &&this.phone.substr(0, 2) != 17 && this.phone.substr(0, 2) != 18 && this.phone.substr(0, 2) != 15 && this.phone.substr(0, 2) != 18 && this.phone.substr(0, 3) != 157`` ||this.phone.length != 11) {
        this.$message({
          type: 'error',
          message: "手机号格式不正确"
        })
        return;
      }
      //已经发送过短信，就不允许再次发送
      if (this.isSend) {
        return;
      }
      this.axios.get("http://localhost:9090/sms/sendSmsCode?phoneNumber=" + this.phone).then(function (response) {
        if (response.data.code==1){
          this.$message({
            type: 'success',
            message: "短信发送成功"
          })
        } else if (response.data.code==2){
          this.$message({
            type: 'error',
            message: "短信发送失败"
          })
        }
      })
      //改变短信发送状态和按钮状态, 60秒内不重复发送
      this.isSend = true;
      this.changeStyle(60);
    },
    changeStyle(time) {
      if (time <= 0) {
        this.sendMsg = "发送短信"
      }else {
        this.sendMsg = time

        setTimeout(()=>{
          // time--;
          this.changeStyle(this.timeCount --);
        },1000)
        // setTimeout("this.changeStyle(" + time +")", 1000);
      }
    },
    checkSms() {
      this.axios.get("http://localhost:9090/sms/checkSmsCode?phoneNumber=" + this.phone + "&smsCode=" + this.smsCode)
          .then(function (response) {
            if (response.data.code==1){
              this.$message({
                type: 'success',
                message: "短信验证成功"
              })
            } else if (response.data.code==2){
              this.$message({
                type: 'error',
                message: "短信验证失败"
              })
            }
          })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // byUsername() {
    //   /**
    //    * 查询满足查询条件的用户列表
    //    */
    //   this.axios.post("http://localhost:9090/system/users/isByUsername", this.input).then((response) => {
    //     this.tableData = response.data;
    //   })
    // },



  }



  }


</script>
<style>

.welcome{color: #f8bace;}
body{
  margin: 0;
  background-image: url(" ../assets/hah.jpg");
  background-size: cover;
  text-align: center;
  overflow: auto;
}

img{width: 150px; align-content: center}



</style>
